let { getToken, verifyToken } = require('../router/Token/index')
const interceptor = (req, res, next) => {
    // console.log(req.originalUrl);
    const reg = new RegExp("/api")
    if (reg.test(req.originalUrl)) {
        // console.log('包含api需验证token', req.headers.token)
        let data = verifyToken(req.headers.token);
        if (data.code == 200) {
            // console.log(data.data.data.userID,req.body.userID);
            // console.log(req.url);
            if (data.data.data.userID == req.body.userID) {
                next();
            }else{
                res.send({
                    code: 404,
                    msg:"用户信息错误，请重新登入"
                })
            }
        } else {
            res.send({
                code: 400,
                msg: "token字段过期，或不存在"
            })
        }
    } else {
        next();
    }
}

module.exports = interceptor
